<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片视图区域-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input  placeholder="请输入搜索内容"
                     v-model="queryInfo.query"
                     clearable
                     @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search"
                       @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary"
                     @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsList" border  stripe  style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="价格(元)" width="95px"></el-table-column>
        <el-table-column prop="goods_weight"label="重量" width="70px"></el-table-column>
        <el-table-column prop="add_time" label="时间" width="140px"></el-table-column>
        <el-table-column prop="add_ft" label="时间" width="140px">
          <template slot-scope="scope">
            {{scope.row.add_ft | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column  label="操作" width="130px">
          <template slot-scope="scope">
            <!--修改-->
            <el-tooltip effect="dark" content="编辑" placement="top">
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            </el-tooltip>
            <!--删除-->
            <el-tooltip effect="dark" content="删除" placement="top">
              <el-button type="danger" icon="el-icon-delete" size="mini"
              @click="removeGoodsById(scope.row.goods_id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[3, 5, 10, 20, 30]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

  </div>
</template>

<script>
  let goodsjson=require('./json/goods.json')

  export default {
    name: "List.vue",
    data(){
      return{
        queryInfo:{
          query:'',
          pageNum:1,
          PageSize:10
        },
        goodsList:[],
        total:0,
        addDialogVisible:false
      }
    },
    created(){
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(){
        this.goodsList=goodsjson
        console.log(this.goodsList)
        this.total=100
        return
        const {data:res}=await this.$http.get(`goods`,{
            params:this.queryInfo
        })

        if (res.meta.status!==200){
          return this.$message.error('获取商品信息失败')
        }
        this.goodsList=res.data
        this.total=res.total
        this.$message.success('获取商品信息成功')
      },
      handleSizeChange(newSize){
        this.queryInfo.pageSize=newSize
        this.getGoodsList()
      },
      handleCurrentChange(newPage){
        this.queryInfo.pageNum=newPage
        this.getGoodsList()
      },
      async removeGoodsById(id){


       const confirmResult=await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        if (confirmResult!=='confirm'){
          return this.$message.info('取消了删除')
        }
        return
        const {data:res}=await this.$http.delete('goods/${id}');
        if(res.meta.status!==200){
          return this.$message.error('删除失败')
        }
        this.$message.success('删除成功')
        this.getGoodsList()
      },
      //通过路由进行定位页面,路由跳转
      goAddPage(){
        this.$router.push(`/goods/add`)
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
